package com.myapp.ruyixing.ui;

import static android.view.View.GONE;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.myapp.ruyixing.R;
import com.myapp.ruyixing.http.RYHttpClient;
import com.myapp.ruyixing.http.RYHttpResponseHandler;
import com.myapp.ruyixing.model.Model;
import com.myapp.ruyixing.ui.widget.TitleBar;
import com.myapp.ruyixing.utils.LoginHelper;

public class ModifyUserActivity extends Activity implements OnClickListener {

	String nickname;
	String livecity;
	String cityid;
	String sex;

	private RYHttpResponseHandler mHttpResponseHandler = new RYHttpResponseHandler() {

		@Override
		public void onSuccess(Object o) {
			try {
				Model model = (Model) o;
				onLoadingDataFinished(true, model);
			} catch (Exception e) {
				onLoadingDataFinished(false, null);
			}
		}

		@Override
		public void onFailure(Throwable throwable) {
			onLoadingDataFinished(false, null);
		}
	};

	private void showLoading(boolean show, String msg) {
		if (isFinishing()) {
			return;
		}
		if (show) {
			findViewById(R.id.layout_loading).setVisibility(View.VISIBLE);
			((TextView) findViewById(R.id.text_loading)).setText(msg);
		} else {
			findViewById(R.id.layout_loading).setVisibility(GONE);
		}
	}

	private void showToast(String msg) {
		Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
	}

	private void onLoadingDataStarted(String bduss, String nickname, int sex,
			String cityid) {
		showLoading(true, "加载中");
		RYHttpClient.doModifyUser(this, mHttpResponseHandler, bduss, nickname,
				sex, cityid);
	}

	private void onLoadingDataFinished(boolean succeed, Model model) {
		showLoading(false, "加载中");
		if (succeed && model != null && model.errno == 0) {
			showToast("修改成功");
			LoginHelper.getInstance().onModifyUser(nickname, sex, livecity,
					cityid);
			finish();
		} else {
			showToast("修改失败！ " + (model == null ? "" : model.msg));
		}
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_modify_user);
		((TitleBar) findViewById(R.id.title_bar))
				.setOnIconClickedListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						onBackPressed();
					}
				});
		((TitleBar) findViewById(R.id.title_bar))
				.setOnRightIconClickedListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						sex = ((TextView) findViewById(R.id.text_sex))
								.getText().toString();
						nickname = ((TextView) findViewById(R.id.edit_nickname))
								.getText().toString();
						onLoadingDataStarted(LoginHelper.getInstance()
								.getBDUSS(), nickname, "男".equals(sex) ? 1 : 2,
								cityid);
					}
				});

		sex = LoginHelper.getInstance().getUserSex();
		livecity = LoginHelper.getInstance().getUserCity();
		nickname = LoginHelper.getInstance().getUserName();
		cityid = LoginHelper.getInstance().getUserCityId();

		((EditText) findViewById(R.id.edit_nickname)).setText(nickname);
		((TextView) findViewById(R.id.text_city)).setText(livecity);
		((TextView) findViewById(R.id.text_sex)).setText(sex);
		findViewById(R.id.text_city).setOnClickListener(this);
		findViewById(R.id.text_sex).setOnClickListener(this);
		findViewById(R.id.btn_delete).setOnClickListener(this);

	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (requestCode == 888 && resultCode == RESULT_OK) {
			cityid = data.getStringExtra("cityid");
			livecity = data.getStringExtra("cityname");
			((TextView) findViewById(R.id.text_city)).setText(livecity);
		}
		super.onActivityResult(requestCode, resultCode, data);
	}

	@Override
	public void onClick(View arg0) {
		switch (arg0.getId()) {
		case R.id.text_city:
			Intent intent = new Intent(this, CityListActivity.class);
			intent.putExtra("modify_livecity", true);
			startActivityForResult(intent, 888);
			break;

		case R.id.text_sex:
			String sex = ((TextView) findViewById(R.id.text_sex)).getText()
					.toString();
			((TextView) findViewById(R.id.text_sex))
					.setText("女".equals(sex) ? "男" : "女");
			break;

		case R.id.btn_delete:
			((EditText) findViewById(R.id.edit_nickname)).setText("");
			break;

		default:
			break;
		}
	}

}
